import 'package:hive/hive.dart';

class HiveUtil {
  /// 打开 box
  static Future<Box<T>> openBox<T>(String boxName) async {
    return await Hive.openBox<T>(boxName);
  }

  /// 存储数据
  static Future<void> put<T>(String boxName, String key, T value) async {
    final box = await openBox<T>(boxName);
    await box.put(key, value);
  }

  /// 获取数据
  static Future<T?> get<T>(
    String boxName,
    String key, {
    T? defaultValue,
  }) async {
    final box = await openBox<T>(boxName);
    return box.get(key, defaultValue: defaultValue);
  }

  /// 删除数据
  static Future<void> delete<T>(String boxName, String key) async {
    final box = await openBox<T>(boxName);
    await box.delete(key);
  }

  /// 清空 box
  static Future<void> clear<T>(String boxName) async {
    final box = await openBox<T>(boxName);
    await box.clear();
  }

  /// 获取所有 key
  static Future<List<dynamic>> keys<T>(String boxName) async {
    final box = await openBox<T>(boxName);
    return box.keys.toList();
  }

  /// 获取所有 value
  static Future<List<T>> values<T>(String boxName) async {
    final box = await openBox<T>(boxName);
    return box.values.toList().cast<T>();
  }
}
